import { RegisterComponent } from "@feng3d/ecs";
import { PMREMGenerator } from "../../thirdparty/three/imports";
import { Component3D } from "../core/Component3D";
import { WebGLRenderer3D } from "../renderers/WebGLRenderer3D";
import { WebGLRenderTarget3D } from "../renderers/WebGLRenderTarget3D";
import { Scene3D } from "../scenes/Scene3D";

declare module "@feng3d/ecs"
{
    interface ComponentMap { PMREMGenerator3D: PMREMGenerator3D; }
}

/**
 * 包装`three`中`PMREMGenerator`。
 */
@RegisterComponent({ name: 'PMREMGenerator3D' })
export class PMREMGenerator3D extends Component3D
{
    _pmremGenerator: PMREMGenerator;

    renderer: WebGLRenderer3D;

    init(): void
    {
        super.init();

        this._pmremGenerator = new PMREMGenerator(this.renderer?._webGLRenderer);
    }

    destroy()
    {
        this._pmremGenerator = null;
        this.renderer = null;
    }

    fromScene(scene: Scene3D, sigma?: number, near?: number, far?: number)
    {
        const target = this._pmremGenerator.fromScene(scene._scene, sigma, near, far);

        const webGLRenderTarget3D = new WebGLRenderTarget3D();
        webGLRenderTarget3D._target = target;

        return webGLRenderTarget3D;
    }
}
